package cn.com.callback

import cn.com.elite.EliteBean
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

public class HST_ZSKTX implements   ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(HST_ZSKTX.class);
    @Override
    public String doCallBack(EliteBean eliteBean, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {

        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json;charset=utf-8");
        String result="";
        //查询知识库文章表 审核通过的、驳回的 还有没提醒的
        List ZskBHList=eliteBean.chaxun_sql("select  k.ARTICLE_GUID, author_id, ARTICLE_TITLE,reject_reason,AUDITSTATUS from km_article k LEFT JOIN hst_leadsnotice hl on k.ARTICLE_GUID=hl.ARTICLE_GUID where (AUDITSTATUS ='1' or AUDITSTATUS ='3') and (hl.ARTICLE_GUID is null or hl.ARTICLE_GUID ='') ","dataSource");
        //判断集合有数据就继续
        if(ZskBHList.size()>0){
            for (int i = 0; i < ZskBHList.size(); i++) {
                Map ZskBHMap = ZskBHList.get(i);
                String ARTICLE_GUID= ZskBHMap.get("ARTICLE_GUID"); //文章GUID
                String ARTICLE_TITLE=  ZskBHMap.get("ARTICLE_TITLE");//文章标题
                String reject_reason=  ZskBHMap.get("reject_reason"); //驳回原因
                String author_id= ZskBHMap.get("author_id"); //创建人
                String AUDITSTATUS=  ZskBHMap.get("AUDITSTATUS");  //文章类型 1.通过 3.驳回
                List rolegroupList =eliteBean.chaxun_sql("select DEFAULTROLEGROUP from staff where staff_id ='"+author_id+"'","dataSource");
                if(rolegroupList.size()>0) {
                    Map rolegroupMap = rolegroupList.get(0);
                    String rolegroupid=  rolegroupMap.get("DEFAULTROLEGROUP");
                    //通过
                    if(AUDITSTATUS=="1"){
                        String YesMsg="您标题为【"+ARTICLE_TITLE+"】知识库文章审核已通过！";
                        try {
                            int InsertNum=eliteBean.update("INSERT INTO hst_leadsnotice" +
                                    "(noticeId,noticeGroup,noticeBy," +
                                    "createGroup,createby,noticeContent," +
                                    "createddate,limitCount,loadCount,noticeType,ARTICLE_GUID)" +
                                    "VALUES " +
                                    "(UUID()," +
                                    "'"+rolegroupid+"'," +
                                    "'"+author_id+"'," +
                                    "'SYSTEM'," +
                                    "'SELITE'," +
                                    "'"+YesMsg+"'," +
                                    "NOW()," +
                                    "1,0,'20','"+ARTICLE_GUID+"')","dataSource");
                            if (InsertNum>0){
                                result ="已新增通知,文章ID"+ARTICLE_GUID;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            log.debug("新增通知表异常");
                        }
                        //驳回
                    }else if (AUDITSTATUS=="3"){
                        String NoMsg="您标题为【"+ARTICLE_TITLE+"】知识库已被驳回！\n 驳回原因："+reject_reason;
                        int InsertNum= 0;
                        try {
                            InsertNum = eliteBean.update("INSERT INTO hst_leadsnotice" +
                                    "(noticeId,noticeGroup,noticeBy," +
                                    "createGroup,createby,noticeContent," +
                                    "createddate,limitCount,loadCount,noticeType,ARTICLE_GUID)" +
                                    "VALUES " +
                                    "(UUID()," +
                                    "'"+rolegroupid+"'," +
                                    "'"+author_id+"'," +
                                    "'SYSTEM'," +
                                    "'SELITE'," +
                                    "'"+NoMsg+"'," +
                                    "NOW()," +
                                    "1,0,'20','"+ARTICLE_GUID+"')","dataSource");
                            if (InsertNum>0){
                                result ="已新增通知,文章ID"+ARTICLE_GUID;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }


                    }
                }
            }
        }else {
            result ="【没有知识库提醒】";
        }
















        return result;
    }
}
